$(function() {
//Declare variable
var 	tips = $( ".validateTips" ),
		button_name="",
		url="a_main.php", 
		ID="",
		mode="",
		rowSelect="";
	
		
	
		if  ($("#cboBranch").length > 0 ){
			$.get('a_stock.php', {"c":"b"},function(data){
				//alert(data);
				$.each(eval('('+data+')'), function(index, text) {
					$("#cboBranch").append(
							$('<option></option>').val(text).html(index)
						 );
				});
			});	
			$("#cboBranch").append($('<option></option>').val(0).html('All'));
		}
		
		LoadData(1,true);
		
		//Show Loading bar			
		function showLoader(){
 
			$( "#resultData tbody:first" ).prepend( "<tr>"
						+"<td colspan='11'  class='search-background'>"
						+"<label><img src='./images/ajax-loader.gif'/></label>"
						+"</td>"
						+"</tr>" ); 
			 $('.search-background').fadeIn(900,0);
		}
		//Hide Loading bar			
		function hideLoader(){
			$('.search-background').fadeOut(300);
			$( "#resultData tbody tr" ).remove();
		};
		
		
		
		//**********************
		// Search 
		//**********************
		$("#btnSearch").click(function(){
			LoadData (1,true)
		});
		
		//**********************
		//View product detail
		//**********************
		$(".btnViewProduct").live('click',function(){
			var idate="";
			var ibranch="";
			idate= $(this).parent().parent().find("td").eq(1).html();
			ibranch=$(this).parent().parent().find("td").eq(2).html();
			$("#ibr").val(ibranch);
			$("#ipos").val(idate);
			$("#view").val('product');
			$("#Act").submit();
		});
		$("#btnBack").click(function(){
			$("#Act").attr('action','pos.php');
			$("#Act").submit();
		});
		//**********************
		//View customer 
		//**********************
		$(".btnViewCustomer").live('click',function(){
			var ibranch="";
			idate= $(this).parent().parent().find("td").eq(1).html();
			ibranch=$(this).parent().parent().find("td").eq(2).html();
			$("#ibr").val(ibranch);
			$("#ipos").val(idate);
	 
			$("#view").val('customer');
			$("#Act").submit();
		});		
		//b=false , don't gennerate paging
		function LoadData(page,b){
			$( "#resultData tbody tr" ).remove();
			//Show loading
			showLoader();
			//set criteria
			var branch=0;
			
			if ($("#cboBranch").length>0)
				branch=$("#cboBranch").val();
	
			 
			 //Post 			
			var data={};
			data={'m':'pos_history'
				,'datefrom':$("#txtDateFrom").val()
				,'dateto':$("#txtDateTo").val()
				,'barnchid':branch
				,'page':page};
						
			$.post(url,data,function(data){
			// alert(data);
				var obj=eval("(" + data + ")");
				//hide loading
				hideLoader();			
				//set data to table
				$( "#resultData tbody" ).append(obj['data']);
				//set row 
				$("#rowCount").attr('innerHTML',obj['rows']);
				//set page
				if (b){
 
					if (parseInt(obj['totalpage'])>1){
						$("#paging_btn").show();
						setPaging(parseInt(obj['totalpage']));
					}else{
						$("#paging_btn").hide();
					}					
					 
				}
				//$("#"+page).css({'background-color' : '#006699'});
			});
			
		}	
	
	function setPaging(iTotal){
				 $("#paging_btn").paginate({
				count 		: iTotal,
				start 		: 1,
				display     : 10,
				border					: false,
				text_color  			: '#888',
				background_color    	: '#EEE',	
				text_hover_color  		: 'blue',
				background_hover_color	: '#CFCFCF',
				mouse					: 'press',
				onChange     			: function(page){
											LoadData(page,false);
										  }
			});
		}
});
	